package com.kedou.sdk.ui.assist;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

/**
 * Created by Administrator on 2018/1/3 0003.
 */

public class SensorSpecoalEventListener implements SensorEventListener, Handler.Callback {
    private static final String TAG = SensorSpecoalEventListener.class.getSimpleName();

    private static final int SENSOR_MSG_SHAKE_AND_SHAKE = 0;
    private static final int SENSOR_MSG_TURN_AND_TURN = 1;

    private static final long MSG_DELAY_MILLIS = 300L;

    private static final int TRIGGER_EVENT_MINIMUM_PERIOD = 3000;

    private final SensorSpecialFunctionListener listener;
    private final Handler mSensorHandler;

    private long lastTriggerEventTime = 0;

    public SensorSpecoalEventListener(SensorSpecialFunctionListener listener) {
        this.listener = listener;
        this.mSensorHandler = new Handler(Looper.getMainLooper(), this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (isTurnAndTurnCellPhone(event)) {
            mSensorHandler.removeMessages(SENSOR_MSG_TURN_AND_TURN);
            mSensorHandler.removeMessages(SENSOR_MSG_SHAKE_AND_SHAKE);
            mSensorHandler.sendEmptyMessageDelayed(SENSOR_MSG_TURN_AND_TURN, MSG_DELAY_MILLIS);
        } else if (isShakeAndShakeCellPhone(event)) {
            mSensorHandler.removeMessages(SENSOR_MSG_TURN_AND_TURN);
            mSensorHandler.removeMessages(SENSOR_MSG_SHAKE_AND_SHAKE);
            mSensorHandler.sendEmptyMessageDelayed(SENSOR_MSG_SHAKE_AND_SHAKE, MSG_DELAY_MILLIS);
        } else {
           // WL.i(TAG, "onSensorChanged ----> unknown function.");
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case SENSOR_MSG_TURN_AND_TURN:
                if (listener != null && !isFastTriggerEvent()) {
                    listener.onTurnAndTurnCellphone();
                }
                break;

            case SENSOR_MSG_SHAKE_AND_SHAKE:
                if (listener != null && !isFastTriggerEvent()) {
                    listener.onShakeAndShakeCellphone();
                }
                break;

            default:
        }
        return true;
    }

    private boolean isFastTriggerEvent() {
        final long time = System.currentTimeMillis();

        if((time - lastTriggerEventTime) <= TRIGGER_EVENT_MINIMUM_PERIOD){
            return true;
        }
        lastTriggerEventTime = System.currentTimeMillis();
        return false;
    }

    /**
     * 判断手机是否触发了摇一摇功能
     *
     * @param event
     * @return 如果手机触发了摇一摇功能，返回true，否则返回false
     */
    private boolean isShakeAndShakeCellPhone(SensorEvent event) {
        int sensorType = event.sensor.getType();
        float[] values = event.values;
        final int offset = 25;
        if (Sensor.TYPE_ACCELEROMETER == sensorType) {
            if ((Math.abs(values[0]) > offset
                    || Math.abs(values[1]) > offset
                    || Math.abs(values[2]) > offset)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断手机是否触发了翻一翻功能
     *
     * @param event
     * @return 如果手机触发了翻一翻功能，返回true，否则返回false
     */
    private boolean isTurnAndTurnCellPhone(SensorEvent event) {
        int sensorType = event.sensor.getType();
        float[] values = event.values;

        if (Sensor.TYPE_ACCELEROMETER == sensorType) {
            if ((Math.abs(values[0]) < 1
                    && Math.abs(values[1]) < 1
                    && Math.abs(values[2]) > 0)) {
                return true;
            }
        }
        return false;
    }
}
